<?php
namespace Admin\Controller\Blog;
use Admin\Controller\Auto\AutoController;;
class TypeController extends AutoController {
	public static function getIndex($keyword){
		$m=M('b_type');
		if(!empty($keyword)){
			$where['a.type_name']=array('LIKE','%'.$keyword.'%');
			// $where['a.id']=array('LIKE','%'.$keyword.'%');
			// $where['a.id']=$keyword;
			// $where['a.type_name']=$keyword;
			// $where['_logic'] = 'OR';
			// $where='(a.name like %'.$keyword.'%)  OR (a.id like %'.$keyword.'%)';
			// $res=$m->where($where)->select();
		}
		$res=$m->alias('a')
		->field(array('a.*,b.type_name as pname,concat(a.path,"-",a.id) as apath'))
		->join('left join think_b_type b on a.pid=b.id')
		->where($where)
		->order('apath')
		->select();
		foreach($res as $k=>&$v){
			$v['type_name']=str_repeat('|__',count(explode('-',$v['apath']))-1).$v['type_name'];
		}		
		return $res;
	}
	public function index(){
		
		// $this->list=$res;
		$this->assign('list',self::getIndex());
		$this->display('/blog/type/index');
	}
	public function insert(){
		$m=M('b_type');
		$path=$m->where('id='.I('post.pid'))->field('path')->find();
		$path=$path['path'].'-'.I('post.pid');
		$_POST['path']=$path;
		$_POST['id']=null;
		$res=$m->add($_POST);
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function update(){
		$m=M('b_type');
		$res=$m->where('id='.I('post.id'))->save(['type_name'=>I('post.type_name')]);
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function delete(){
		if(getZi(I('post.id'))=='yes'){
			$m=M('b_type');
			$res=$m->delete(I('post.id'));
			if($res){
				echo 'yes';
			}else{
				echo 'no';
			}
		}else{
			echo 'canot';
		}
	}
	public function select(){
		var_dump(self::getIndex(I('post.keyword')));
	}
}
?>